package rssclient;

// Clases base
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;

// Clases para la configuracion de Log4j
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;

public class leerRSS extends JFrame 
{

        String newline = "\n";
        static final Logger log = Logger.getLogger(leerRSS.class.getName());
        static final String LOG_PROPERTIES_FILE = "conf/log4j.properties";  
        public static Boolean valConn;

        public leerRSS() 
        {

                initializeLogger();
	initComponents();

        }

        @SuppressWarnings("unchecked")
 
        private void initComponents() 
        {

                log.info("Inicializando los componentes del frame de la aplicación.");
  	
	jTextField1 = new JTextField();
	jButton1 = new JButton();
	jComboBox1 = new JComboBox();
	jLabel1 = new JLabel();
	jSeparator1 = new JSeparator();
	jLabel3 = new JLabel();
	jScrollPane1 = new JScrollPane();
	jTextArea1 = new JTextArea();
	jButton2 = new JButton();
	jButton3 = new JButton();
	jLabel4 = new JLabel();
	jButton4 = new JButton();
		
                setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                jButton1.setText("Agregar");
                jButton1.addActionListener(new ActionListener() 
                {
                    public void actionPerformed(ActionEvent evt) 
                    {
                        jButton1ActionPerformed(evt);
                    }
                });

                jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

                jLabel1.setText("URL");

                jTextField1.addActionListener(new ActionListener() 
                {
                    public void actionPerformed(ActionEvent evt) 
                    {
                        jTextField1ActionPerformed(evt);
                    }
                });

                jLabel3.setFont(new Font("Tahoma", 1, 11)); // NOI18N
                jLabel3.setText("Manejo Feed");

                jTextArea1.setColumns(20);
                jTextArea1.setRows(5);
                jScrollPane1.setViewportView(jTextArea1);

                jButton2.setText("Limpiar");
                jButton2.addActionListener(new ActionListener() 
                {
                    public void actionPerformed(ActionEvent evt) 
                    {
                        jButton2ActionPerformed(evt);
                    }
                });

                jButton3.setText("Salir");
                jButton3.addActionListener(new ActionListener() 
                {
                    public void actionPerformed(ActionEvent evt) 
                    {
                        jButton3ActionPerformed(evt);
                    }
                });

                jLabel4.setVisible(true);
	jLabel4.setText(" ");

                jButton4.setText("Cargar");
	jButton4.addActionListener(new ActionListener() 
                {
                        public void actionPerformed(ActionEvent evt) 
                        {
                                jButton4ActionPerformed(evt);
                        }
                });

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
					.addGroup(layout.createSequentialGroup()
						.addComponent(jButton2)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jButton3))
					.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
						.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE)
						.addComponent(jSeparator1)
						.addGroup(layout.createSequentialGroup()
							.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
								.addComponent(jLabel3)
								.addComponent(jLabel1))
							.addGap(34, 34, 34)
							.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
								.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
								.addComponent(jComboBox1, 0, 373, Short.MAX_VALUE)
								.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
							.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
								.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
				.addGap(23, 23, 23))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGap(15, 15, 15)
				.addComponent(jLabel3)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(jButton1)
					.addComponent(jLabel1))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(jButton4))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(jLabel4)
				.addGap(18, 18, 18)
				.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(jButton2)
					.addComponent(jButton3))
				.addContainerGap())
		);

                                pack();
        
        }

        private void initializeLogger() 
        {

                Properties logProperties = new Properties();

                try
	{

                        // carga nuestro archivo log4j properties / archivo de configuracion
                        logProperties.load(new FileInputStream(LOG_PROPERTIES_FILE));
                        PropertyConfigurator.configure(logProperties);
                        log.info("Inicializando Logging.");
	
                }
	catch(IOException e)
	{

                        throw new RuntimeException("No es posible generar el archivo de logs " + LOG_PROPERTIES_FILE);
        
                }
        }

        private void jTextField1ActionPerformed(ActionEvent evt) 
        {
        
                // TODO add your handling code here:
	//jTextArea1.append(extraeRSS.extraer() + newline);
    
        }

        private void jButton1ActionPerformed(ActionEvent evt) 
        {
                // TODO add your handling code here:
        }

        private void jButton2ActionPerformed(ActionEvent evt) 
        {
                // TODO add your handling code here:
        }

        private void jButton3ActionPerformed(ActionEvent evt) 
        {

                log.info("Se presiona el botón salir\n");
	Container Frame = jButton3.getParent();
	do Frame = Frame.getParent();
	while (!(Frame instanceof JFrame));
	((JFrame) Frame).dispose();

        }

        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
        {
        
                jTextArea1.append(extraeRSS.extraer() + newline);

                if(valConn)
                {

                        jLabel4.setText("Conexion satisfactoria");
                        
                }
                else
                {
                        
                        infoBox("No es posible establecer la conexion con la url.\nEl programa será finalizado." , "Error");
                        System.exit(0);
                        
                }
    
        }

        public static void infoBox(String infoMessage, String location)
        {
        
    
                JOptionPane.showMessageDialog(null, infoMessage, "Tipo de mensaje: " + location, JOptionPane.INFORMATION_MESSAGE);
    
        }

        
        public static void main(String args[]) 
        {

                log.info("Inicializando el Look&Feel de la aplicación");
        
                try 
                {
            
                        for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) 
                        {
                
                                if ("Nimbus".equals(info.getName())) 
                                {
                    
                                        UIManager.setLookAndFeel(info.getClassName());
                                        break;
                                        
                                }
                        }
                        
                } 
                catch (ClassNotFoundException ex) 
                {
            
                        log.fatal("Error en el despliegue del form de la aplicación\n" +  ex.getMessage(), ex);
        
                } 
                catch (InstantiationException ex) 
                {
            
                        log.fatal("Error en el despliegue del form de la aplicación\n" + ex.getMessage(), ex);
        
                } 
                catch (IllegalAccessException ex) 
                {
           
                        log.fatal("Error en el despliegue del form de la aplicación\n" + ex.getMessage(), ex);
        
                } 
                catch (UnsupportedLookAndFeelException ex) 
                {
            
                        log.fatal("Error en el despliegue del form de la aplicación\n" + ex.getMessage(), ex);
        
                }

                log.info("Creando y desplegando la ventana");
                EventQueue.invokeLater(new Runnable() {

                        public void run() 
                        {
                                new leerRSS().setVisible(true);
                        }
                });
		
	log.info("Fin de la aplicación");	

        }
    // Declaración de variables
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JComboBox jComboBox1;
    private JLabel jLabel1;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JScrollPane jScrollPane1;
    private JSeparator jSeparator1;
    private JTextArea jTextArea1;
    private JTextField jTextField1;
    // Fin de la declaración de variables
}

